package com.online.dao.user.Impl;

import com.mysql.cj.x.protobuf.MysqlxCrud;
import com.online.dao.user.ShopDao;
import com.online.pojo.UpComing_Shop;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;


public class ShopDaoImpl implements ShopDao {

    public boolean insertShop(String shop_name, String user_name, String pwd, String legal_p,String tel) {
        int rs;
        boolean falg = false;
        try {
            Connection connection = BaseDao.getConnection();
            String sql = "insert into online.upcoming_shop(shop_name, user_name, pwd, legal_p ,tel) VALUES (?,?,?,?,?)";
            PreparedStatement pstm = connection.prepareStatement(sql);
            pstm.setString(1, shop_name);
            pstm.setString(2, user_name);
            pstm.setString(3, pwd);
            pstm.setString(4, legal_p);
            pstm.setString(5, tel);
            rs = pstm.executeUpdate();
            if (rs != 0) {
                falg = true;
                System.out.println("提交审批成功");
            } else {
                falg = false;
                System.out.println("提交审批失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return falg;
    }

    public List<UpComing_Shop> selectShop(List<UpComing_Shop> list) throws SQLException {
        ResultSet rs;
        Connection connection = BaseDao.getConnection();
        String sql ="select * from online.upcoming_shop";
        PreparedStatement pstm = connection.prepareStatement(sql);
        rs = pstm.executeQuery();
        int i=0;
        while (rs.next()){
            UpComing_Shop shop = new UpComing_Shop();
            shop.setShop_name(rs.getString(1));
            shop.setUser_name(rs.getString(2));
            shop.setPwd(rs.getString(3));
            shop.setLegal_p(rs.getString(4));
            shop.setTel(rs.getString(5));
            list.add(i,shop);
            i++;

        }
        System.out.println(list);
        pstm.close();
        connection.close();
        return list;
    }

    public boolean insertmerchant(String shop_name, String user_name, String pwd, String legal_p, String tel, int account) throws SQLException {
        int rs;
        boolean falg = false;
        try {
            Connection connection = BaseDao.getConnection();
            String sql = "insert into online.merchant(shop_name, user_name, pwd, legal_p, tel, account) VALUES (?,?,?,?,?,?)";
            PreparedStatement pstm = connection.prepareStatement(sql);
            pstm.setString(1, shop_name);
            pstm.setString(2, user_name);
            pstm.setString(3, pwd);
            pstm.setString(4, legal_p);
            pstm.setString(5, tel);
            pstm.setDouble(6,account);
            rs = pstm.executeUpdate();
            if (rs != 0) {
                falg = true;
                System.out.println("入驻成功");
            } else {
                falg = false;
                System.out.println("入驻失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return falg;
    }

    public UpComing_Shop select(String name) throws SQLException {
        ResultSet rs;
        UpComing_Shop shop=new UpComing_Shop();
        Connection connection = BaseDao.getConnection();
        String sql ="select * from online.upcoming_shop where shop_name=?;";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setString(1,name);
        rs= pstm.executeQuery();
        if (rs.next()){
            shop.setShop_name(rs.getString(1));
            shop.setUser_name(rs.getString(2));
            shop.setPwd(rs.getString(3));
            shop.setLegal_p(rs.getString(4));
            shop.setTel(rs.getString(5));
            System.out.println(shop);
        }
        System.out.println(shop);
        return shop;
    }

    public boolean del(String name) throws SQLException {
        boolean falg;
        int rs;
        Connection connection = BaseDao.getConnection();
        String sql ="delete from online.upcoming_shop where shop_name=?;";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setString(1,name);
        rs= pstm.executeUpdate();
        if(rs>0){
            falg=true;
            System.out.println("已删除");
        }else {
            falg=false;
        }
        pstm.close();
        connection.close();
        return falg;
    }
}
